app.controller("bookinfo", function ($http, $scope, $location) {
        $scope.user = JSON.parse(sessionStorage.getItem("user"));
        console.log($scope.user);
        $scope.bookinfo = {};
        if (JSON.parse(sessionStorage.getItem("book")) != null) {
            $scope.bookinfo = JSON.parse(sessionStorage.getItem("book"));

            if ($scope.bookinfo.categoryId == 1) {
                $scope.bookinfo.categoryId = "科学";
            }
            if ($scope.bookinfo.categoryId == 2) {
                $scope.bookinfo.categoryId = "植物";
            }
            if ($scope.bookinfo.categoryId == 4) {
                $scope.bookinfo.categoryId = "电学";
            }
            if ($scope.bookinfo.categoryId == 5) {
                $scope.bookinfo.categoryId = "传记";
            }
            $scope.bookinfoimg = $scope.bookinfo.bookimg
        } else {
            $scope.bookinfo = null;
            $("#modal-191634").click();
            $('.modal-title').html("获取数据失败");
            $('.modal-body').html("未找到您的相关信息,请检查您的信息是否填写有误");
            $location.path("/index-content");
        }
        console.log($scope.bookinfo);
        $scope.reader = new FileReader();
        var categoryid;
        $scope.gtecategory = function ($event) {
            categoryid = $($event.target).attr("id");
            $('#dropdownMenu1').html($($event.target).html());
            console.log(categoryid);
        };
        $scope.bookCategory = null;
        $http({
            method: 'Get',
            url: app.url + 'Book/Category'
        }).then(function (res) {
            console.log(res.data);
            $scope.bookCategory = res.data;
        }, function () {
            $('.dropdownMenu1').val("未查到数据");
        });

        $scope.img64 = null;
        function getObjectURL(file) {
            var url = null;
            if (window.createObjectURL != undefined) { // basic
                url = window.createObjectURL(file);
            } else if (window.URL != undefined) { // mozilla(firefox)
                url = window.URL.createObjectURL(file);
            } else if (window.webkitURL != undefined) { // webkit or chrome
                url = window.webkitURL.createObjectURL(file);
            }
            return url;
        }

        $scope.img_upload = function (files) {
            $scope.reader.readAsDataURL(files[0]);  //FileReader的方法，把图片转成base64
            var objUrl = getObjectURL(files[0]);
            $('#pic').attr('src', objUrl);
        };

        $scope.reader.onload = function (ev) {
            $scope.$apply(function () {
                var img = ev.target.result;//接收base64
                $scope.bookinfoimg = img;
                img = img.replace("data:image/png;base64,", '');
                img = img.replace("data:image/jpg;base64,", '');
                img = img.replace("data:image/jpeg;base64,", '');
                $scope.bookinfo.bookimg = img;
            });
        };

        $scope.updateBook = function () {
            console.log($scope.bookimg);
            if ($scope.bookinfo.categoryId == "科学") {
                $scope.bookinfo.categoryId = 1;
            }
            if ($scope.bookinfo.categoryId == "植物") {
                $scope.bookinfo.categoryId = 2;
            }
            if ($scope.bookinfo.categoryId == "电学") {
                $scope.bookinfo.categoryId = 4;
            }
            if ($scope.bookinfo.categoryId == "传记") {
                $scope.bookinfo.categoryId = 5;
            }
            $http({
                method: 'POST',
                url: app.url + 'Book/update',
                data: $scope.bookinfo,

            }).then(function (res) {
                console.log(res.data);
                $location.path('/index-content');
            }, function () {
                $('.dropdownMenu1').val("未查到数据");
            })
        };
        $scope.BorrowBooks = function (id) {
            function nowtime() {//将当前时间转换成yyyymmdd格式
                var mydate = new Date();
                var str = "" + mydate.getFullYear();
                var mm = mydate.getMonth() + 1
                if (mydate.getMonth() > 9) {
                    str += "-" + mm;
                }
                else {
                    str += "-" + "0" + mm;
                }
                if (mydate.getDate() > 9) {
                    str += "-" + mydate.getDate();
                }
                else {
                    str += "-" + "0" + mydate.getDate();
                }

                // if (mydate.getHours()> 9) {
                //     str += " " + mydate.getHours();
                // }
                // else {
                //     str += " " + "0" + mydate.getHours();
                // }
                // if (mydate.getMinutes() > 9) {
                //     str += ":" + mydate.getHours();
                // }
                // else {
                //     str += ":" + "0" + mydate.getMinutes();
                // }


                return str;
            };
            console.log(nowtime());
            var books = {
                'bookId': id,
                'studentId': $scope.user.studentId,
                'nowtime':nowtime()

            };

            $http({
                method: 'POST',
                url: app.url + 'Book/borrowbook',
                data:books

            }).then(function (res) {
                console.log(res.data);
                if (res.data != 0) {
                    $location.path('/index-content')
                }
                else {
                    $("#modal-191634").click();
                    $('.modal-title').html("借书失败");
                    $('.modal-body').html("我也不知道怎么回事，找下管理员");
                }
            }, function () {

            });
            console.log(books)
        }
    }
);